---
title: "Bram_AgreementSurvey"
author: "Curtis Bram"
output: pdf_document
---

```{r package, echo = F, include=F}

knitr::opts_chunk$set(fig.width=4.5, fig.height=5) 

rm(list=ls())


options(scipen=999)

  pkgs <- c("dplyr", "ggplot2", "stargazer", "car", "tidyverse", "extrafont", "scales", "plyr", "caret", "coefplot", "lavaan", "xtable", "formattable")
# A function to load all above packages. Install if they have not been installed.
  usePackage <- function(p){
    for (pkg in p){
      if (!is.element(pkg, installed.packages()[,1]))
        install.packages(pkg, dep = TRUE, repos = "https://cloud.r-project.org/")
      require(pkg, character.only = TRUE)
    }
  }
  usePackage(pkgs)
```


```{r , echo = T, include = T, warning = F}
set.seed(1234567)

#loadfonts()



# Load Data and Code Variables

d0 <- read.csv("dataNum.csv")

summary(d0$POST > 100)
summary(d0$POST <0)

## This is to exclude the 9 people who entered impossible answers since I forgot 
## to include content validation. It also clears out all the people who got 
## kicked for failing the attention check
d1 <- subset(d0, d0$POST >-1 & d0$POST <101)

d <- as.data.frame(d1)
## When I was looking through excel I saw a few people who got the exact right 
## answer before and after, I suspect they found a way to take the survey 2x since there are a few repeat 
## emails in the emails survey. I made a variable for these people but did not exclude them from the analysis.
d$Cheat <- ifelse(d$PRE == 31 & d$POST == 31, 1, 0)
summary(as.factor(d$Cheat))

## Pre-Post Comparison

d$VotePercentPP <- (d$PostVote - d$PreVote)/100
d$FtBidenPP <- (d$PostFT_1 - d$PreFT_1)/100
d$FtTrumpPP <- (d$PostFT_2 - d$PreFT_2)/100
d$FtDemocratsPP <- (d$PostFT_7 - d$PreFT_7)/100
d$FtRepublicansPP <- (d$PostFT_8 - d$PreFT_8)/100
d$CandDiffPP <- (d$PostCandDiff - d$PreCandDiff)/4
d$ElectImportantPP <- (d$PostImp - d$PreImp)/4
d$AgreementPP <- (d$POST - d$PRE)/100
d$PreTreatmentEstimate <- d$PRE /100
d$PostTreatmentEstimate <- d$POST /100
d$DisagreementPP <- d$AgreementPP/-1
d$PostTreatmentDisagreement <- 1- d$PostTreatmentEstimate
d$PreTreatmentDisagreement <- 1 - d$PreTreatmentEstimate

## What people saw

d$AgreeTreatment <- ifelse(d$Condition == 'c', 1, 0)
d$DisagreeTreatment <- ifelse(d$Condition == 'd', 1, 0)
d$NumAgreeIssues <- d$TotalAgreeStatements
d$NumDisagreeIssues <- d$TotalDisagreeStatements
d$TotalIssues <- d$TotalStatements

d$ShareAgreeIssues <- (d$NumAgreeIssues / d$TotalIssues)
d$ShareDisagreeIssues <- (d$NumDisagreeIssues / d$TotalIssues)

## OutCandidate FT

d$Democrat <- ifelse(d$political_party == 1 |d$political_party == 2 | d$political_party == 3 |d$political_party == 6, 1, 0)
d$Republican <- ifelse(d$political_party == 5 |d$political_party == 9 | d$political_party == 10 | d$political_party == 8, 1, 0)



d$OutcandidateFT0 <- ifelse(d$Democrat == 1, d$FtTrumpPP, 'NA')
d$OutcandidateFtPP <- as.numeric(ifelse(d$OutcandidateFT0 == 'NA' & d$Republican == 1, d$FtBidenPP, d$OutcandidateFT0))

## Affecitve Polarization

d$OutPartyFT0 <- ifelse(d$Democrat == 1, d$FtRepublicansPP, 'NA')
d$OutPartyFtPP <- as.numeric(ifelse(d$OutPartyFT0 == 'NA' & d$Republican == 1, d$FtDemocratsPP, d$OutPartyFT0))

d$InPartyFT0 <- ifelse(d$Democrat == 1, d$FtDemocratsPP, 'NA')
d$InPartyFtPP <- as.numeric(ifelse(d$InPartyFT0 == 'NA' & d$Republican == 1, d$FtRepublicansPP, d$InPartyFT0))

d$AffectivePolPP <- (d$InPartyFtPP - d$OutPartyFtPP)/2

# Partisan identity

d$DemIdentity <- (d$DemImp + d$DemWell + d$DemTalking + d$DemExtent)/17
d$RepIdentity <- (d$RepImp + d$RepWell + d$RepTalking + d$RepExtent)/17

d$PartisanIdentity0 <- ifelse(d$Democrat == 1, d$DemIdentity, 'NA')
d$PartisanIdentity <- as.numeric(ifelse(d$PartisanIdentity0 == 'NA' & d$Republican == 1, d$RepIdentity, d$PartisanIdentity0))

## PARTISAN IDENTITY WITH NONIDENTIFIERS AT 0 

d$PartisanIdentity2 <- ifelse(is.na(d$PartisanIdentity) == T, 0, d$PartisanIdentity)


## SAMPLE PLOTS

ShareDissPlot <- ggplot(data = d, aes(ShareDisagreeIssues)) + geom_histogram(binwidth = .2) +  
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(x= "Share of Disagreement In Respondent's Observed Sample", y = "Number of Respondents", title = "Observed Share of Disagreement") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(plot.title = element_text(hjust = 0.5)) +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
#ShareDissPlot


TotalIssuesPlot <- ggplot(data = d, aes(TotalIssues)) + geom_histogram(binwidth = 1) +   
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(x= "Number of Issues In Respondent's Observed Sample", y = "Number of Respondents", title = "Number of Issues Seen") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(plot.title = element_text(hjust = 0.5)) +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
#TotalIssuesPlot




## Demographics

## 1 = 18-29; 2= 30-49; 3 = 50 - 69; 4 = 70+
summary(as.factor(d$Age))
d$Age0 <- d$Age
d$Age <- d$age

## 1 = White; 2 = Black; 3 = Hispanic; 4 = Asian; 5 = Other / Multiple
summary(as.factor(d$Race))

d$Black <- ifelse(d$Race == 2, 1, 0)
d$White <- ifelse(d$Race == 1, 1, 0)
d$Hispanic <- ifelse(d$Race == 3, 1, 0)

## 0 = Female, 1 = Male 
summary(as.factor(d$Sex))
d$Male <- ifelse(d$Sex == 1, 1, 0)

## -3 = Extremely Conservative; 3 = Extremely Liberal
summary(as.factor(d$Ideology))


## Democrats
summary(as.factor(d$Democrat))

## Republicans
summary(as.factor(d$Republican))

## 1 = D Term; 2 = B Term; 3 = Expressive Vote; 4 = P Term; 5 = C Term
summary(as.factor(d$WhyVote))



## Education: 1 = LT HS; 2 = High school; 3 = Other post HS; 4 = Some college; 5 = AS; 6 = BA; 7 = MA; 8 = PhD
d$Education <- as.numeric(ifelse(d$education == -3105 | d$education == 10, "NA", d$education))
summary(as.factor(d$Education))

## Income (increasing), 1 = less than 15k; 24 = more than 250k

d$Income <- as.numeric(ifelse(d$hhi == -3105, "NA", d$hhi))

## Region: 1 = Northeast; 2 = Midwest; 3 = South; 4 = West

d$Region <- d$region


## PARTISAN STRENGTH

d$PartisanStrength0 <- as.numeric(ifelse(d$political_party == 1, 4, "NA"))
d$PartisanStrength1 <- as.numeric(ifelse(d$political_party == 2, 3, d$PartisanStrength0))
d$PartisanStrength2 <- as.numeric(ifelse(d$political_party == 3, 2, d$PartisanStrength1))
d$PartisanStrength3 <- as.numeric(ifelse(d$political_party == 4, 0, d$PartisanStrength2))
d$PartisanStrength4 <- as.numeric(ifelse(d$political_party == 5, -2, d$PartisanStrength3))
d$PartisanStrength5 <- as.numeric(ifelse(d$political_party == 6, 1, d$PartisanStrength4))
d$PartisanStrength6 <- as.numeric(ifelse(d$political_party == 7, 0, d$PartisanStrength5))
d$PartisanStrength7 <- as.numeric(ifelse(d$political_party == 8, -1, d$PartisanStrength6))
d$PartisanStrength8 <- as.numeric(ifelse(d$political_party == 9, -3, d$PartisanStrength7))
d$PartisanStrength <- as.numeric(ifelse(d$political_party == 10, -4, d$PartisanStrength8))

## STRONG PARTISAN

d$StrongPartisan <- ifelse(d$PartisanStrength == 4 | d$PartisanStrength == -4, 1, 0)

## IDEOLOGUE

d$Liberal <- ifelse(d$Ideology > 0, d$Ideology, 0)
d$Conservative <- ifelse(d$Ideology <0, d$Ideology, 0)


## RESCALE 0 to 1

d$PreCandDiff2 <- (as.numeric(d$PreCandDiff)-1)/4
d$PostCandDiff2 <- (as.numeric(d$PostCandDiff)-1)/4

## PARTISAN / NO LEAN / LEANER

d$Partisan <- ifelse(d$Democrat == 1 | d$Republican == 1, 1, 0)

d$PartisanNoLean <- ifelse( d$political_party != 6 & d$political_party != 8 & d$political_party != 3 & d$political_party != 5, d$Partisan, 0)

d$Leaner2 <- ifelse(d$political_party == 6 | d$political_party == 8 | d$political_party == 3 |d$political_party == 5, 1, 0)

d$Ideologue <- ifelse(d$Ideology > 0 | d$Ideology <0 | d$Ideology2 >0 | d$Ideology2 <0 , 1, 0)

#stargazer(subset(d, select=c("White","Black", "Hispanic", "age", "Male", "Ideology", "Democrat", "Republican", "Education", "Income")), digits = 2)


```






```{r , echo = T, include = F, warning = F}
## Models

H1 <- lm(DisagreementPP ~ DisagreeTreatment, data =d)



H1 <- lm(DisagreementPP ~ DisagreeTreatment  , data = d)

H1.a <- lm(DisagreementPP ~ ShareDisagreeIssues   , data = d)

H2.a <- lm(CandDiffPP ~ DisagreeTreatment , data = d)

H2.b <- lm(VotePercentPP ~ DisagreeTreatment , data = d)

H2.c <- lm(OutcandidateFtPP ~ DisagreeTreatment  , data = d)

H2.d <- lm(ElectImportantPP ~ DisagreeTreatment  , data = d)

AffectivePolarization <- lm(AffectivePolPP ~ DisagreeTreatment  , data = d)


#tbl1 <- table(mean(d$PostCandDiff2, na.rm=T), d$DisagreeTreatment, na.rm=T)
#tbl1
#chisq.test(tbl1)


## GAM 
## These are basically the same
#g1 <- gam(AgreementPP ~ DisagreeTreatment, data =d)
#summary(g1)

#kfold <- cv.lm(data=d, form.lm=H2.c, m= 10, plotit=F)

train.control <- trainControl(method = "cv", number = 10)
# Train the model
k1 <- train(DisagreementPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)

k2 <- train(CandDiffPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)

k3 <- train(VotePercentPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)

k4 <- train(OutcandidateFtPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)

k5 <- train(ElectImportantPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)

k6 <- train(AffectivePolPP ~ DisagreeTreatment, data = d, method = "lm",
               trControl = train.control,  na.action = na.omit)
# Summarize the results
print(k3)
summary(k1)




```
```{r, results = 'asis', echo = F, message = F}
#stargazer(H1, H1.a, header = F)
#stargazer(H2.a, H2.b,  H2.d, header = F)
#stargazer(H2.c, AffectivePolarization, header = F)
```


```{r, include = T, echo = F, warning =F}
## MAIN RESULTS

#d$DisagreeTreatment2<-as.numeric(d$DisagreeTreatment)-1
plot0 <- ggplot(d, aes(ShareDisagreeIssues, PostTreatmentDisagreement)) + geom_smooth(method = lm, color = "black") + scale_y_continuous(labels = percent_format(accuracy = 1)) + scale_x_continuous(labels=scales::percent) +
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(x= "Share of Disagreement In Respondent's Observed Sample", y = "Post-Treatment Estimate of Candidate Disagreement", title = "What percent of Britannica's issues do you \n think Donald Trump and Joe Biden disagree on?")+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10")) +annotate("text", x = .3, y = .735, label = "73% = Pre-treatment estimated disagreement", size = 3, family = "LM Roman 10") + annotate("text", x = .8, y = .685, label = "69% = True share disagreement", size = 3, family = "LM Roman 10") +  geom_hline(yintercept = .73) + geom_hline(yintercept=.69, linetype="dashed", 
                color = "black", size=1) +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) 
#plot0


loessplot <- ggplot(d, aes(ShareDisagreeIssues, PostTreatmentDisagreement)) + geom_smooth( method = loess, color = "black") + scale_y_continuous(labels = percent_format(accuracy = 1)) + scale_x_continuous(labels=scales::percent) +
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(x= "Share of Disagreement In Respondent's Observed Sample", y = "Post-Treatment Estimate of Candidate Disagreement", title = "What percent of Britannica's issues do you \n think Donald Trump and Joe Biden disagree on?")+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10")) +annotate("text", x = .3, y = .735, label = "73% = Pre-treatment estimated disagreement", size = 3, family = "LM Roman 10") + annotate("text", x = .8, y = .685, label = "69% = True share disagreement", size = 3, family = "LM Roman 10") +  geom_hline(yintercept = .73) + geom_hline(yintercept=.69, linetype="dashed", 
                color = "black", size=1) +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) 
#loessplot



my.labels <- c( "Not different \n at all", "A little \n different", "Somewhat \n different",   "Very \n different", "Extremely \n different")


d$PostCandDiff<-as.factor(d$PostCandDiff)
d$DisagreeTreatment <- as.factor(d$DisagreeTreatment)



library(tidyverse)
library(formattable)

detach("package:plyr", unload = TRUE)
tidydata <- d %>%
  group_by(DisagreeTreatment, PostCandDiff) %>%
  summarise(count = n()) %>%
  mutate(freq = formattable::percent(count / sum(count)))

##add SD##
d$PostCandDiff<-as.numeric(d$PostCandDiff)
tidydata$sd<-sd(d$PostCandDiff)
treato <- 32+61+200+300+384
treatone <- 15+16+72+220+643

tidydata$averages<-ifelse(tidydata$DisagreeTreatment == 0,(tidydata$count / 977)*100,(tidydata$count / 966)*100)

tidydata$conf.low<-tidydata$averages - 1.96*tidydata$sd
tidydata$conf.high<-tidydata$averages + 1.96*tidydata$sd

tidydata$PostCandDiff[tidydata$PostCandDiff == 1] <- "Not different \n at all"
tidydata$PostCandDiff[tidydata$PostCandDiff == 2] <- "A little \n different"
tidydata$PostCandDiff[tidydata$PostCandDiff == 3] <- "Somewhat \n different"
tidydata$PostCandDiff[tidydata$PostCandDiff == 4] <- "Very \n different"
tidydata$PostCandDiff[tidydata$PostCandDiff == 5] <- "Extremely \n different"

hist<-ggplot(tidydata, aes(x = PostCandDiff, fill = DisagreeTreatment, y = averages)) +geom_bar(stat = "identity", position = "dodge") + geom_errorbar(aes(ymin=conf.low, ymax=conf.high), width=.2,position=position_dodge(.9)) + theme(text = element_text(size=10, family="Garamond")) + labs(x="", y = "Percent of Respondents Per Condition", title = "How different do you think Joe Biden and \n Donald Trump are?", fill = "Condition") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(plot.title = element_text(hjust = 0.5)) +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black")) + theme(legend.position = "bottom")+ scale_color_grey() +scale_fill_grey(labels = c("Agree", "Disagree"))+ scale_x_discrete(limits = c("Not different \n at all", "A little \n different", "Somewhat \n different",   "Very \n different", "Extremely \n different"))

ggsave(filename="histogram.jpeg", plot=hist, device="jpeg",  height=5, width=5, units="in", dpi=500)





```


```{r, include = T, echo = T}

## COMPARISON TO PARTISANSHIP / IDEOLOGY


modx <- lm(PreCandDiff2 ~ DisagreeTreatment + Leaner2+  PartisanNoLean + StrongPartisan+ Ideologue , data = d)
mody <- lm(PostCandDiff2 ~ DisagreeTreatment + Leaner2 + PartisanNoLean + StrongPartisan+ Ideologue , data = d)



modq <- lm(PreTreatmentDisagreement ~ DisagreeTreatment + Leaner2+  PartisanNoLean + StrongPartisan+ Ideologue , data = d)
modw <- lm(PostTreatmentDisagreement ~ DisagreeTreatment + Leaner2+  PartisanNoLean + StrongPartisan+ Ideologue, data = d)




# coefplot 
plotx<-coefplot::multiplot(modx, mody,  dodgeHeight = .6, color = "black", zeroColor = "black", fillColor = "black", single = T, ncol =1,intercept = F, title ="", legend.position = "bottom",  names = c( modx = "Pre-Treatment", mody = "Post-Treatment"), legend.reverse = T) + scale_color_manual(values=c("black","grey")) +
    theme(axis.text.y = element_blank(), axis.line.y = element_blank(), axis.ticks.y=element_blank())+ 
    theme(text         = element_text(size=10, family="LM Roman 10")) + labs(title = "How different do you think Joe Biden and Donald Trump are?" , x= "Coefficient", y = "")+ 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))+ annotate("text", x = -.025, y = 1, label = "Disagree Condition", size = 3, family = "LM Roman 10") +annotate("text", x = -.025, y = 2, label = "Leaner", size = 3, family = "LM Roman 10")+annotate("text", x = -.025, y = 3, label = "Weak Partisan", size = 3, family = "LM Roman 10") + annotate("text", x = -.025, y =4, label = "Strong Partisan", size = 3, family = "LM Roman 10") + annotate("text", x = -.038, y =4, label = "", size = 3, family = "LM Roman 10")+ annotate("text", x = -.025, y =5, label = "Ideologue", size = 3, family = "LM Roman 10")
plotx  
ggsave(filename="plotx.jpeg", plot=plotx, device="jpeg",  height=5, width=5, units="in", dpi=500)

plotq<-coefplot::multiplot(modq, modw,  color = "black", dodgeHeight = 0.6, zeroColor = "black", fillColor = "black", single = T, ncol =1,intercept = F, title ="",
 legend.position = "bottom", names = c(modw = "Pre-Treatment", modq = "Post-Treatment")) + scale_color_manual(values=c("black","grey")) +
  theme(axis.text.y = element_blank(), axis.line.y = element_blank(), axis.ticks.y=element_blank())+ 
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(title = "What percent of Britannica's issues do you \n think Donald Trump and Joe Biden Disagree on?" , x= "Coefficient", y = "")+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+ annotate("text", x = -.04, y = 1, label = "Disagree Condition", size = 3, family = "LM Roman 10") +annotate("text", x = -.04, y = 2, label = "Leaner", size = 3, family = "LM Roman 10")+annotate("text", x = -.04, y = 3, label = "Partisan", size = 3, family = "LM Roman 10") + annotate("text", x = -.04, y =4, label = "Strong Partisan", size = 3, family = "LM Roman 10") + annotate("text", x = --.05, y =4, label = "", size = 3, family = "LM Roman 10")+ annotate("text", x = -.04, y =5, label = "Ideologue", size = 3, family = "LM Roman 10")



## COMPARISON TO PARTISAN IDENTITY


modq2 <- lm(PreTreatmentDisagreement ~ DisagreeTreatment + PartisanIdentity2 , data = d)
modw2 <- lm(PostTreatmentDisagreement ~ DisagreeTreatment + PartisanIdentity2, data = d)


modx2 <- lm(PreCandDiff2 ~ DisagreeTreatment+ PartisanIdentity2 , data = d)
mody2 <- lm(PostCandDiff2 ~ DisagreeTreatment+ PartisanIdentity2 , data = d)

#stargazer(modq2, modw2,  modx2, mody2, header = F, type = "html", out = "/Users/curtisbram/Library/Mobile Documents/com~apple~CloudDocs/Duke /Projects/Finished Projects/Most Important Election/Data analysis/table3.doc")

## InferTable

subset5 <- subset(d, d$AgreeTreatment == 1)

InferredDisAgreeCond <- (20- subset5$TotalAgreeStatements)/20

subset6 <- subset(d, d$AgreeTreatment == 0)

InferredDisDisCond <- subset6$TotalDisagreeStatements/20



inferplot <- ggplot(subset5, aes(DisagreeTreatment, InferredDisAgreeCond)) + geom_boxplot() + geom_boxplot(data = subset6, aes(DisagreeTreatment, InferredDisDisCond))
#inferplot

plotq2<-coefplot::multiplot(modq2, modw2,  color = "black", dodgeHeight = 0.6, zeroColor = "black", fillColor = "black", single = T, ncol =1,intercept = F, title ="",
 legend.position = "bottom", names = c(modw = "Pre-Treatment", modq = "Post-Treatment")) + scale_color_manual(values=c("black","grey")) +
  theme(axis.text.y = element_blank(), axis.line.y = element_blank(), axis.ticks.y=element_blank())+ 
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(title = "What percent of Britannica's issues do you \n think Donald Trump and Joe Biden Disagree on?" , x= "Coefficient", y = "")+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+ annotate("text", x = -.04, y = 1, label = "Disagree Condition", size = 3, family = "LM Roman 10") +annotate("text", x = -.04, y = 2, label = "Partisan Social Identity", size = 3, family = "LM Roman 10")

ggsave(filename="plotq2.jpeg", plot=plotq2, device="jpeg", height=5, width=5, units="in", dpi=500)

plotw2<-coefplot::multiplot(modx2, mody2,  color = "black", dodgeHeight = 0.6, zeroColor = "black", fillColor = "black", single = T, ncol =1,intercept = F, title ="",
 legend.position = "bottom", names = c(modw = "Pre-Treatment", modq = "Post-Treatment")) + scale_color_manual(values=c("black","grey")) +
  theme(axis.text.y = element_blank(), axis.line.y = element_blank(), axis.ticks.y=element_blank())+ 
   theme(text         = element_text(size=10, family="LM Roman 10")) + labs(title = "How different do you think Joe Biden and Donald Trump are?"  , x= "Coefficient", y = "")+ 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10"))  +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+ annotate("text", x = .05, y = 1, label = "Disagree Condition", size = 3, family = "LM Roman 10") +annotate("text", x = .05, y = 2, label = "Partisan Social Identity", size = 3, family = "LM Roman 10")



```


```{r, include = T, echo = T}
#Extra Analysis

EducationModel <- lm(DisagreementPP ~ ShareDisagreeIssues*Education, data = d)

EduPlot <- sjPlot::plot_model(EducationModel, type =c("int"), int.term = "ShareDisagreeIssues*Education", show.ci= T, mdrt.values = "minmax", show.legend = F, color = "black")  + theme(text         = element_text(size=10, family="LM Roman 10")) + labs(x= "Share of Disagreement In Respondent's Observed Sample", y = "Change In Estimated Share Disagreement", title = "")+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + theme(plot.title = element_text(hjust = 0.5, family = "LM Roman 10")) + annotate("text", x = .25, y = 0.03, label = "Highest Level Education", size = 3, family = "LM Roman 10") + annotate("text", x = .84, y = -0.06, label = "Lowest Level Education", size = 3, family = "LM Roman 10") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) 
#EduPlot



## Interactions

int1 <- lm(DisagreementPP ~ DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)

int2 <- lm(CandDiffPP ~ DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)
int3 <- lm(VotePercentPP ~ DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)
int4 <- lm(OutcandidateFtPP ~DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)
int5 <- lm(ElectImportantPP ~DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)
int6 <- lm(AffectivePolPP ~DisagreeTreatment*Democrat + DisagreeTreatment*Republican + DisagreeTreatment*Ideology, data = d)

## DEMOGRAPHICS AND BALANCE CHECKING

BalanceCheck <- lm(DisagreeTreatment ~ PreTreatmentEstimate + PreCandDiff + PreImp + PreVote + PastVote+  Democrat + Republican   + Age + Education + Male + Black + White + Hispanic + Ideology  , data =d )

Demo1 <- lm(DisagreementPP ~ ShareDisagreeIssues +Age + Education + Male + Race + Ideology + Democrat + Republican, data =d)

Demo1.a <- lm(DisagreementPP ~ DisagreeTreatment +Age + Education + Male + Race + Ideology + Democrat + Republican   , data = d)


Demo2.a <- lm(CandDiffPP ~ DisagreeTreatment +Age + Education + Male + Race + Ideology +  Democrat + Republican      , data = d)

Demo2.b <- lm(VotePercentPP ~ DisagreeTreatment +  Age + Education + Male + Race + Ideology +  Democrat + Republican    , data = d)

Demo2.c <- lm(OutcandidateFtPP ~ AgreeTreatment + Age + Education + Male + Race + Ideology + Democrat + Republican, data = d )

Demo2.d <- lm(ElectImportantPP ~ DisagreeTreatment + Age + Education + Male + Race + Ideology +  Democrat + Republican , data = d)

DemoAffectivePolarization <- lm(AffectivePolPP ~ AgreeTreatment + Age + Education + Male + Race + Ideology +  Democrat + Republican , data = d )



```

```{r, results = 'asis', echo = F}


#stargazer(Demo1, Demo1.a, header = F)
#stargazer(Demo2.a, Demo2.b, Demo2.d, header = F)
#stargazer(Demo2.c, DemoAffectivePolarization, header = F)
#stargazer(BalanceCheck, header = F)

sem1 <- '
VotePercentPP ~ 1 + a*DisagreeTreatment + b*DisagreementPP
DisagreementPP ~ 1 + c*DisagreeTreatment
IndirectEffect := b*c
'
fitsem <- sem(sem1, data=d)
summary(fitsem)

sem2 <- 'OutcandidateFtPP ~ 1+ a*DisagreeTreatment + b*DisagreementPP
DisagreementPP ~ 1+c*DisagreeTreatment
IndirectEffect := b*c
'
fitsem2 <- sem(sem2, data=d)
summary(fitsem2)

sem3 <- '
ElectImportantPP ~ 1 + a*DisagreeTreatment + b*DisagreementPP
DisagreementPP ~ 1 + c*DisagreeTreatment
IndirectEffect := b*c
'
fitsem3 <- sem(sem3, data=d)
summary(fitsem3)

sem4 <- 'AffectivePolPP ~ 1 + a*DisagreeTreatment + b*DisagreementPP
DisagreementPP ~ 1 + c*DisagreeTreatment
IndirectEffect := b*c
'
fitsem4 <- sem(sem4, data=d)
summary(fitsem4)

xtable(parameterEstimates(fitsem)) 

```




